package com.lzy.business.controller.sys;

import com.lzy.base.apis.SysResourceServiceApi;
import com.github.pagehelper.PageInfo;
import com.lzy.base.constants.Constants;
import com.lzy.base.dtos.SysResourceDto;
import com.lzy.base.utils.UserUtil;
import com.lzy.business.request.SysResourceListRequest;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DateUtil;
import com.lzy.framework.core.utils.DozerUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lc
 * @date 2017年1月1日 下午6:05:19
 */
@Controller
@RequestMapping("admin/resource")
public class SysResourceController{
    @Autowired
    private SysResourceServiceApi sysResourceServiceApi;

    /**
     * 默认页面
     **/
    @RequestMapping(method = RequestMethod.GET)
    public String resourceList() {
        return "admin/resourceList";
    }

    /**
     * 菜单页面
     **/
    @RequestMapping(value = "menu", method = RequestMethod.GET)
    public String menuList() {
        return "admin/menuList";
    }

    /**
     * 菜单集合 JSON
     **/
//    @RequiresPermissions("sys:res:menu:view")
    @RequestMapping(value = "menu/json", method = RequestMethod.GET)
    @ResponseBody
    public List<SysResourceDto> menuData1(@RequestParam("id") Long pid) {
    	if(pid==0){
    		List<SysResourceDto> sysMenus = sysResourceServiceApi.selectAllResource(null,
                    Constants.RESOURCE_ADMIN_MENU, pid, null, null);
        	return sysMenus;
        }else{
        	List<SysResourceDto> list1 =new ArrayList<SysResourceDto>();
        	List<SysResourceDto> list = getChilden(pid,list1);
        	return list;
        }
    }

    @RequestMapping(value = "first/json", method = RequestMethod.GET)
    @ResponseBody
    public List<SysResourceDto> menuFirst(@RequestParam("id") Long pid) {
        if(pid==0){
            List<SysResourceDto> sysMenus = sysResourceServiceApi.selectFirstListResource(null,
                    Constants.RESOURCE_ADMIN_MENU, pid, null, null);
            return sysMenus;
        }else{
            List<SysResourceDto> list1 =new ArrayList<SysResourceDto>();
            List<SysResourceDto> list = getChilden(pid,list1);
            return list;
        }
    }
    
    public List<SysResourceDto> getChilden(Long id,List<SysResourceDto> list){
    	List<SysResourceDto> sysMenus = sysResourceServiceApi.selectAllResource(null,
                Constants.RESOURCE_ADMIN_MENU, id, null, null);
    	if(null!=sysMenus){
    	for(SysResourceDto a : sysMenus){
    		List<SysResourceDto> sysMenus1 = sysResourceServiceApi.selectAllResource(null,
                    Constants.RESOURCE_ADMIN_MENU, a.getId(), null, null);
    		if(null==sysMenus1){
    			list.add(a);
    		}else{
    			getChilden(a.getId(),list);
    		}
    		
    	}
    	}
    	return list;
    }
    
    /**
     * 按钮集合 JSON
     **/
//    @RequiresPermissions("sys:res:menu:view")
    @RequestMapping(value = "func/json", method = RequestMethod.GET)
    @ResponseBody
    public List<SysResourceDto> funcData(@RequestParam("id") Long pid) {
        List<SysResourceDto> sysMenus = sysResourceServiceApi.selectAllResource(null,
                Constants.RESOURCE_BUTTON, pid, null, null); // UserUtil.getCurrentUser().getDataType());
        return sysMenus;
    }

    /**
     * 获取菜单下的操作
     **/
//    @RequiresPermissions("sys:res:view")
    @RequestMapping("opera/json")
    @ResponseBody
    public PageBean menuOperationDate(SysResourceListRequest request) {
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        SysResourceDto resource = null;
        if (request != null)
            resource = DozerUtils.map(request, SysResourceDto.class);
        PageInfo<SysResourceDto> pageInfo = sysResourceServiceApi.queryResourceByPid(pager, resource);
        Ensure.that(pageInfo).isNotNull("01001108");
        return new PageBean(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 权限集合 JSON
     **/
//    @RequiresPermissions("sys:res:view")
    @RequestMapping(value = "json", method = RequestMethod.GET)
    @ResponseBody
    public List<SysResourceDto> resourceData(@RequestParam("pid") Long pid) {
        List<SysResourceDto> sysResources = sysResourceServiceApi.selectAllResource(
                null, null, pid, Constants.RESOURCE_STATUS_ABLE, null);
        return sysResources;
    }

    /**
     * 权限集合 JSON
     **/
//    @RequiresPermissions("sys:role:resView")
    @RequestMapping(value = "jsonInRole", method = RequestMethod.GET)
    @ResponseBody
    public List<SysResourceDto> jsonInRole(@RequestParam("pid") Long pid) {
        List<SysResourceDto> sysResources = sysResourceServiceApi.selectAllResource(
                null, null, pid, Constants.RESOURCE_STATUS_ABLE, UserUtil
                        .getCurrentUser().getDataType());
        return sysResources;
    }

    /**
     * 添加菜单跳转
     **/
    @RequestMapping(value = "menu/create", method = RequestMethod.GET)
    public String menuCreateForm(@RequestParam("pid") Long pid, Model model) {
        SysResourceDto sysResource = new SysResourceDto();
        sysResource.setPid(pid);
        model.addAttribute("menu", sysResource);
        model.addAttribute("action", "create");
        return "admin/menuForm";
    }

    /**
     * 添加菜单
     **/
//    @RequiresPermissions("sys:res:menu:add")
    @RequestMapping(value = "menu/create", method = RequestMethod.POST)
    @ResponseBody
    public Result createMenu(@Valid SysResourceDto sysResource) {
        Result result = new Result();
        sysResource.setType(0);
        sysResource.setOperateTime(DateUtil.formatDateTime(new Date()));
        sysResourceServiceApi.insert(sysResource,
                Constants.RESOURCE_ADMIN_MENU);
        return result;
    }

    /**
     * 添加权限跳转
     **/
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(@RequestParam("pid") Long pid, Model model) {
        SysResourceDto sysResource = new SysResourceDto();
        sysResource.setPid(pid);
        model.addAttribute("resource", sysResource);
        model.addAttribute("action", "create");
        return "admin/resourceForm";
    }

    /**
     * 添加权限
     **/
//    @RequiresPermissions("sys:res:add")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public Result create(@Valid SysResourceDto sysResource) {
        Result result = new Result();
        sysResource.setDisorder(null);
        sysResource.setType(2);
        sysResource.setOperateTime(DateUtil.format(new Date()));
        sysResource.setCreateUser(UserUtil.getCurrentUser().getId());
        sysResourceServiceApi.insert(sysResource, Constants.RESOURCE_BUTTON);
        return result;
    }

    /**
     * 修改菜单跳转
     **/
    @RequestMapping(value = "menu/update", method = RequestMethod.GET)
    public String menuUpdateForm(@RequestParam("id") Long id, Model model) {
        model.addAttribute("menu", sysResourceServiceApi.selectByPrimaryKey(id));
        model.addAttribute("action", "update");
        return "admin/menuForm";
    }

    /**
     * 修改菜单
     */
//    @RequiresPermissions("sys:res:menu:update")
    @RequestMapping(value = "menu/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateMenu(@Valid @ModelAttribute("resource") SysResourceDto sysResource) {
        Result result = new Result();
        sysResource.setType(0);
        sysResource.setOperateTime(DateUtil.format(new Date()));
        sysResourceServiceApi.update(sysResource,Constants.RESOURCE_ADMIN_MENU);
        return result;
    }

    /**
     * 修改菜单状态
     */
//    @RequiresPermissions("sys:res:menu:updateStatus")
    @RequestMapping(value = "menu/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public Result updateMenuStatus(@RequestParam("id") Long id,@RequestParam("status") int status) {
        Result result = new Result();
        SysResourceDto sysResource = sysResourceServiceApi.selectByPrimaryKey(id);
        sysResource.setStatus(status);
        sysResourceServiceApi.update(sysResource,Constants.RESOURCE_ADMIN_MENU);
        return result;
    }

    /**
     * 修改权限跳转
     */
    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String updateForm(@RequestParam("id") Long id, Model model) {
        model.addAttribute("resource",sysResourceServiceApi.selectByPrimaryKey(id));
        model.addAttribute("action", "update");
        return "admin/resourceForm";
    }

    /**
     * 修改权限
     */
//    @RequiresPermissions("sys:res:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@Valid @ModelAttribute("resource") SysResourceDto sysResource) {
        Result result = new Result();
        sysResource.setDisorder(null);
        sysResource.setType(2);
        sysResource.setOperateTime(DateUtil.format(new Date()));
        sysResourceServiceApi.update(sysResource, Constants.RESOURCE_BUTTON);
        return result;
    }

    /**
     * 修改权限状态
     */
//    @RequiresPermissions("sys:res:updateStatus")
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public Result updateResStatus(@RequestParam("id") Long id,@RequestParam("status") int status) {
        Result result = new Result();
        SysResourceDto sysResource = sysResourceServiceApi.selectByPrimaryKey(id);
        sysResource.setStatus(status);
        sysResourceServiceApi.update(sysResource, Constants.RESOURCE_BUTTON);
        return result;
    }

    /**
     * 删除菜单
     */
//    @RequiresPermissions("sys:res:menu:delete")
    @RequestMapping(value = "menu/delete")
    @ResponseBody
    public Result deleteMenu(@RequestParam("id") Long id) {
        String  updateOk = sysResourceServiceApi.delete(id);
        if(!updateOk.equalsIgnoreCase("OK"))
            Ensure.that(false).isTrue("11020031");
        return new Result();
    }

    /**
     * 删除权限
     */
//    @RequiresPermissions("sys:res:delete")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("id") Long id) {
        String updateOk = sysResourceServiceApi.delete(id);
        if(!updateOk.equalsIgnoreCase("OK"))
            Ensure.that(false).isTrue("01001107");
        return new Result();
    }

}
